package il.technion.cs236369.parsers;

import il.technion.cs236369.exceptions.ParsingException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * An abstract file/stream parsing file
 * @author Gal Lalouche
 *
 * @param <T> The return value the parser returns after parsing
 */
abstract class Parser<T> {
	/**
	 * Parses the stream
	 * @param stream The stream to parse
	 * @return The instance parsed
	 * @throws ParsingException If anything went wrong with the parsing
	 */
	public abstract T parse(InputStream stream) throws ParsingException;
	
	/**
	 * Parses the file
	 * @param file The file to parse
	 * @return The instance parsed
	 * @throws FileNotFoundException If the file was not found
	 * @throws ParsingException If anything went wrong with the parsing
 	 */
	public T parse(File file) throws FileNotFoundException, ParsingException {
		if (file == null) {
			throw new IllegalArgumentException("File is null");
		}
		FileInputStream stream = new FileInputStream(file);
		return parse(stream);
	}
}
